<template>
  <el-container>
    <el-header>
      <div class="logo">
        <img src="../assets/logo.png" alt />
      </div>
      <div class="user">
        <img src="../assets/logo.png" alt />
      </div>
    </el-header>
    <el-container>
      <!-- 左侧菜单栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          :default-active="activePath"
          :collapse="isCollapse"
          router
          unique-opened
          :collapse-transition="false"
          background-color="transparent"
          text-color="#fff"
          active-text-color="#f39800"
        >
          <el-menu-item
            :index="item.address + ''"
            v-for="item in menu_item"
            :key="item.id"
            @click="saveNavState(item.address)"
          >
          <i class="el-icon-star-off"></i>
          <span>{{item.name}}</span>
          </el-menu-item>
          <el-submenu :index="item.address + ''" :key="item.id" v-for="item in submenu_item">
            <template slot="title">
              <i class="el-icon-star-off"></i>
              <span>{{item.name}}</span>
            </template>
            <el-menu-item
              :index="'/' + subItem.address"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.address)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.name}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 左侧菜单栏End -->

      <el-container>
      <!-- 右侧主体区域 -->
      <el-main>
        <!-- 右侧面包屑 -->
        <div class="menu-on">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-button @click="isCollapse = !isCollapse" class="col">
              <i class="el-icon-s-operation"></i>
            </el-button>
            <el-breadcrumb-item to="/home">
              <el-button>首页</el-button>
            </el-breadcrumb-item>
            <el-breadcrumb-item :to="this.activePath">
              <el-button v-if="this.activePath">{{this.bread_crumb[0]['name']}}</el-button>
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <!-- 右侧面包屑End -->

        <!-- 返回顶部按钮:下拉才显示 -->
        <el-backtop target=".el-main"></el-backtop>
        <!-- 返回顶部按钮End -->

        <!-- 右侧视图区 -->
        <router-view></router-view>
        <!-- 右侧视图区End -->
      </el-main>
      <!-- 右侧主体区域End -->

      <!-- 右侧页脚区域 -->
      <el-footer>
          footer
      </el-footer>
      <!-- 右侧页脚区域End -->
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      isCollapse: false,
      menulist: [],
      activePath: ''
    }
  },
  // 页面加载前的事件
  created() {
    // 执行事件Mneu
    this.menu()
    // 获得存储在网页的Session
    this.activePath = window.sessionStorage.getItem('activePath')
    console.log(this.activePath)
  },
  methods: {
    // 获取菜单栏
    async menu() {
      const res = await this.$http.get('menu.php')
      console.log(res)
      this.menulist = res.data
    },
    saveNavState(activePath) {
      // 存储当前页面的Session
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  computed: {
    // 一级菜单
    menu_item() {
      return this.menulist.filter(function (user) {
        return user.children.length <= 0
      })
    },
    // 二级菜单
    submenu_item() {
      return this.menulist.filter(function (user) {
        return user.children.length > 0
      })
    },
    // 面包屑
    bread_crumb() {
      return this.menulist.filter(user => {
        return user.address === this.activePath
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    position: relative;
    line-height: 60px;
    text-align: center;
    background-color: #333;
    img {
      vertical-align: middle;
      max-height: 100%;
    }
    .logo {
      height: 100%;
    }
    .user {
      position: absolute;
      right: 0;
      top: 0;
      height: 100%;
      img {
        height: 80%;
        border: 2px solid #fff;
        border-radius: 50%;
        margin: 0 10px;
      }
    }
  }
  .el-aside {
    transition: 0.5s;
    background: url(../assets/img/menu_bgc.jpg) no-repeat center / cover;
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #f5f5f5;
    padding: 10px;
    .menu-on {
      position: relative;
      height: 40px;
      font-size: 20px;
      .el-breadcrumb {
        position: fixed;
        z-index: 1;
        width: 100%;
        background-color: #f5f5f5;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
        padding: 10px;
        margin: -10px 0 0 -10px;
        .col {
          float: left;
          margin-right: 10px;
        }
      }
    }
  }
  .el-footer {
    height: 40px !important;
    line-height: 40px;
      font-size: 16px;
    text-align: center;
      color: #000;
    background-color: #eee;
  }
}
</style>
